﻿namespace ACCOUNTING.LVY.Business
{
    using ACCOUNTING;
    using Microsoft.VisualBasic.CompilerServices;
    using System;
    using System.Data;
    using System.Runtime.InteropServices;
    using System.Windows.Forms;

    public class clsMenu
    {
        private string mHeThong = "";
        private int mHienThi = 0;
        private int mImageIndex = 0;
        private string mKhoa = "";
        private string mMenuOrder = "";
        private string mNhom = "";
        private int mNhomMenu = 0;
        private string mPhanHe = "";
        private string mTenAnh = "";
        private string mTenViet = "";
        private string mThuocTinh = "";

        public DataTable GetAllMenu(string pPhanHe = "")
        {
            string str = "";
            if (pPhanHe.Trim() != "")
            {
                str = " And PhanHe = '" + pPhanHe.Trim() + "'";
            }
            object obj2 = " SELECT * FROM HT_Menu WHERE 1 = 1 " + str + " ORDER BY MenuOrder";
            return DBHelper.ExecuteDataset(modGeneral.ConnectionString, CommandType.Text, Conversions.ToString(obj2), new IDataParameter[0]).Tables[0];
        }

        public DataTable GetListBaoCao(string pPhanHe = "")
        {
            DataTable table;
            string str = "";
            str = " WHERE ThuocTinh = 'BC'";
            clsNguoiDung dung = new clsNguoiDung();
            dung.Load(modGeneral.H_USERNAME);
            if (dung.KhoaNhanVien != "000000000000000")
            {
                str = str + " AND isNull(SE.F003,0) <> 0";
            }
            if (pPhanHe.Trim() != "")
            {
                str = str + " And CharIndex('" + pPhanHe.Trim() + "', PhanHe) <> 0 ";
            }
            try
            {
                string commandText = "SELECT MN.Khoa, MN.TenVIET as TenMenu, MN.ImageIndex, MN.Nhom, MN.ThuocTinh, MN.PhanHe FROM HT_Menu MN LEFT JOIN HT_Security SE ON RTRIM(SE.F001) = '" + modGeneral.MD5Encrypt(modGeneral.H_USERNAME) + "' AND Rtrim(SE.F002) = MN.Khoa" + str + "  Order by MN.MenuOrder ";
                table = DBHelper.ExecuteDataset(modGeneral.ConnectionString, CommandType.Text, commandText, new IDataParameter[0]).Tables[0];
            }
            catch (Exception exception1)
            {
                ProjectData.SetProjectError(exception1);
                Exception exception = exception1;
                MessageBox.Show(exception.Message.ToString());
                table = null;
                ProjectData.ClearProjectError();
                return table;
                ProjectData.ClearProjectError();
            }
            return table;
        }

        public DataTable GetListMenu(string pPhanHe = "", string strConditions = "")
        {
            DataTable table;
            string str = " WHERE 1 = 1 ";
            if (pPhanHe.Trim() != "")
            {
                str = str + " AND CharIndex('" + pPhanHe.Trim() + "', PhanHe) <> 0 ";
            }
            if (strConditions.Trim() != "")
            {
                str = str + " AND " + strConditions;
            }
            try
            {
                string commandText = "SELECT Khoa, Ten" + modGeneral.H_LANGUAGE + " as TenMenu, ImageIndex, Nhom, ThuocTinh, PhanHe FROM HT_Menu " + str + " Order by MenuOrder ";
                table = DBHelper.ExecuteDataset(modGeneral.ConnectionString, CommandType.Text, commandText, new IDataParameter[0]).Tables[0];
            }
            catch (Exception exception1)
            {
                ProjectData.SetProjectError(exception1);
                Exception exception = exception1;
                MessageBox.Show(exception.Message.ToString());
                table = null;
                ProjectData.ClearProjectError();
                return table;
                ProjectData.ClearProjectError();
            }
            return table;
        }

        public DataTable GetListMenuCon(string pPhanHe = "", string strConditions = "")
        {
            DataTable table;
            string str = " WHERE 1 = 1 ";
            if (pPhanHe.Trim() != "")
            {
                str = str + " AND CharIndex('" + pPhanHe.Trim() + "', PhanHe) <> 0 ";
            }
            clsNguoiDung dung = new clsNguoiDung();
            dung.Load(modGeneral.H_USERNAME);
            if (dung.KhoaNhanVien != "000000000000000")
            {
                str = str + "AND isNull(SE.F003,0) <> 0";
            }
            if (strConditions.Trim() != "")
            {
                str = str + " AND " + strConditions;
            }
            try
            {
                string commandText = "SELECT MN.Khoa, MN.TenVIET as TenMenu, MN.ImageIndex, MN.Nhom, MN.ThuocTinh, MN.PhanHe FROM HT_Menu MN LEFT JOIN HT_Security SE ON RTRIM(SE.F001) = '" + modGeneral.MD5Encrypt(modGeneral.H_USERNAME) + "' AND Rtrim(SE.F002) = MN.Khoa" + str + "  Order by MN.MenuOrder ";
                table = DBHelper.ExecuteDataset(modGeneral.ConnectionString, CommandType.Text, commandText, new IDataParameter[0]).Tables[0];
            }
            catch (Exception exception1)
            {
                ProjectData.SetProjectError(exception1);
                Exception exception = exception1;
                MessageBox.Show(exception.Message.ToString());
                table = null;
                ProjectData.ClearProjectError();
                return table;
                ProjectData.ClearProjectError();
            }
            return table;
        }

        public DataTable GetListPhanHe(string pConditions = "")
        {
            DataTable table;
            string str = "";
            str = " WHERE HienThi = 1 ";
            if (pConditions.Trim() != "")
            {
                str = str + " AND " + pConditions;
            }
            try
            {
                string commandText = "SELECT Ma, Ten" + modGeneral.H_LANGUAGE + " as TenPhanHe, ImgIndex FROM HT_PhanHe " + str + " Order by Stt ";
                table = DBHelper.ExecuteDataset(modGeneral.ConnectionString, CommandType.Text, commandText, new IDataParameter[0]).Tables[0];
            }
            catch (Exception exception1)
            {
                ProjectData.SetProjectError(exception1);
                Exception exception = exception1;
                MessageBox.Show(exception.Message.ToString());
                table = null;
                ProjectData.ClearProjectError();
                return table;
                ProjectData.ClearProjectError();
            }
            return table;
        }

        public DataTable GetListTimKiem(string pPhanHe = "")
        {
            DataTable table;
            string str = "";
            str = " WHERE ThuocTinh = 'TK'";
            if (pPhanHe.Trim() != "")
            {
                str = str + " And CharIndex('" + pPhanHe.Trim() + "', PhanHe) <> 0 ";
            }
            try
            {
                string commandText = "SELECT Khoa, Ten" + modGeneral.H_LANGUAGE + " as TenMenu, ImageIndex, Nhom, ThuocTinh, PhanHe FROM HT_Menu " + str + " Order by MenuOrder ";
                table = DBHelper.ExecuteDataset(modGeneral.ConnectionString, CommandType.Text, commandText, new IDataParameter[0]).Tables[0];
            }
            catch (Exception exception1)
            {
                ProjectData.SetProjectError(exception1);
                Exception exception = exception1;
                MessageBox.Show(exception.Message.ToString());
                table = null;
                ProjectData.ClearProjectError();
                return table;
                ProjectData.ClearProjectError();
            }
            return table;
        }

        public bool Load(string pKhoa)
        {
            bool flag=false;
            IDataParameter[] commandParameters = new IDataParameter[2];
            try
            {
                string commandText = "SELECT * FROM HT_Menu WHERE Khoa = '" + pKhoa + "'";
                IDataReader dataReader = DBHelper.ExecuteReader(modGeneral.ConnectionString, CommandType.Text, commandText, commandParameters);
                if (dataReader.Read())
                {
                    this.mKhoa = Conversions.ToString(modGeneral.GetValue(dataReader, 0, modGeneral.typeOfColumn.GetString));
                    this.mTenViet = Conversions.ToString(modGeneral.GetValue(dataReader, 1, modGeneral.typeOfColumn.GetString));
                    this.mTenAnh = Conversions.ToString(modGeneral.GetValue(dataReader, 2, modGeneral.typeOfColumn.GetString));
                    this.mImageIndex = Conversions.ToInteger(modGeneral.GetValue(dataReader, 3, modGeneral.typeOfColumn.GetInt32));
                    this.mNhom = Conversions.ToString(modGeneral.GetValue(dataReader, 4, modGeneral.typeOfColumn.GetString));
                    this.mThuocTinh = Conversions.ToString(modGeneral.GetValue(dataReader, 5, modGeneral.typeOfColumn.GetString));
                    this.mMenuOrder = Conversions.ToString(modGeneral.GetValue(dataReader, 6, modGeneral.typeOfColumn.GetString));
                    this.mPhanHe = Conversions.ToString(modGeneral.GetValue(dataReader, 7, modGeneral.typeOfColumn.GetString));
                    this.mHeThong = Conversions.ToString(modGeneral.GetValue(dataReader, 8, modGeneral.typeOfColumn.GetString));
                    this.mHienThi = Conversions.ToInteger(modGeneral.GetValue(dataReader, 9, modGeneral.typeOfColumn.GetInt32));
                    this.mNhomMenu = Conversions.ToInteger(modGeneral.GetValue(dataReader, 10, modGeneral.typeOfColumn.GetInt32));
                }
                dataReader.Close();
                flag = true;
            }
            catch (Exception exception1)
            {
                ProjectData.SetProjectError(exception1);
                Exception exception = exception1;
                MessageBox.Show(exception.Message.ToString());
                flag = false;
                ProjectData.ClearProjectError();
                return flag;
                ProjectData.ClearProjectError();
            }
            return flag;
        }

        public bool Save()
        {
            bool flag2;
            IDataParameter[] commandParameters = new IDataParameter[11];
            try
            {
                commandParameters[0] = DBHelper.createParameter("@Khoa", DbType.Int64, ParameterDirection.Input, this.mKhoa);
                commandParameters[1] = DBHelper.createParameter("@TenViet", DbType.Int64, ParameterDirection.Input, this.mTenViet);
                commandParameters[2] = DBHelper.createParameter("@TenAnh", DbType.Int64, ParameterDirection.Input, this.mTenAnh);
                commandParameters[3] = DBHelper.createParameter("@ImageIndex", DbType.Double, ParameterDirection.Input, this.mImageIndex);
                commandParameters[4] = DBHelper.createParameter("@Nhom", DbType.Int64, ParameterDirection.Input, this.mNhom);
                commandParameters[5] = DBHelper.createParameter("@ThuocTinh", DbType.Int64, ParameterDirection.Input, this.mThuocTinh);
                commandParameters[6] = DBHelper.createParameter("@MenuOrder", DbType.Int64, ParameterDirection.Input, this.mMenuOrder);
                commandParameters[7] = DBHelper.createParameter("@PhanHe", DbType.Int64, ParameterDirection.Input, this.mPhanHe);
                commandParameters[8] = DBHelper.createParameter("@HeThong", DbType.Int64, ParameterDirection.Input, this.mHeThong);
                commandParameters[9] = DBHelper.createParameter("@HienThi", DbType.Double, ParameterDirection.Input, this.mHienThi);
                commandParameters[10] = DBHelper.createParameter("@NhomMenu", DbType.Double, ParameterDirection.Input, this.mNhomMenu);
                bool flag = DBHelper.ExecuteNonQuery(modGeneral.ConnectionString, CommandType.StoredProcedure, "HT_sp_Menu", commandParameters) > 0;
                flag2 = true;
            }
            catch (Exception exception1)
            {
                ProjectData.SetProjectError(exception1);
                Exception exception = exception1;
                MessageBox.Show(exception.Message.ToString());
                flag2 = false;
                ProjectData.ClearProjectError();
                return flag2;
                ProjectData.ClearProjectError();
            }
            return flag2;
        }

        public string HeThong
        {
            get
            {
                return this.mHeThong;
            }
            set
            {
                this.mHeThong = value;
            }
        }

        public int HienThi
        {
            get
            {
                return this.mHienThi;
            }
            set
            {
                this.mHienThi = value;
            }
        }

        public int ImageIndex
        {
            get
            {
                return this.mImageIndex;
            }
            set
            {
                this.mImageIndex = value;
            }
        }

        public string Khoa
        {
            get
            {
                return this.mKhoa;
            }
            set
            {
                this.mKhoa = value;
            }
        }

        public string MenuOrder
        {
            get
            {
                return this.mMenuOrder;
            }
            set
            {
                this.mMenuOrder = value;
            }
        }

        public string Nhom
        {
            get
            {
                return this.mNhom;
            }
            set
            {
                this.mNhom = value;
            }
        }

        public int NhomMenu
        {
            get
            {
                return this.mNhomMenu;
            }
            set
            {
                this.mNhomMenu = value;
            }
        }

        public string PhanHe
        {
            get
            {
                return this.mPhanHe;
            }
            set
            {
                this.mPhanHe = value;
            }
        }

        public string TenAnh
        {
            get
            {
                return this.mTenAnh;
            }
            set
            {
                this.mTenAnh = value;
            }
        }

        public string TenViet
        {
            get
            {
                return this.mTenViet;
            }
            set
            {
                this.mTenViet = value;
            }
        }

        public string ThuocTinh
        {
            get
            {
                return this.mThuocTinh;
            }
            set
            {
                this.mThuocTinh = value;
            }
        }
    }
}

